package com.app.reportproblem;

import java.util.Date;
import java.util.HashMap;
import java.util.WeakHashMap;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.app.reportproblem.bean.EventInfo;
import com.app.reportproblem.frgament.MainMapFragement;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;

public class ViewProblem extends Activity implements LocationListener{
	private MainMapFragement mapFragment;
	private HashMap<Marker, EventInfo> eventMarkerMap;
	
	float mLatitude = 0;
	float mLongitude = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_problem);
		mapFragment = new MainMapFragement();
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		ft.add(R.id.map, mapFragment);
		ft.commit();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		setUpEventSpots();
	}
	
	
	private void setUpEventSpots() {
		// I'm going to make 2 EventInfo objects and place them on the map
		EventInfo firstEventInfo = new EventInfo(new LatLng(50.154, 4.35), "Right now - event", new Date(), "Party");
		EventInfo secondEventInfo = new EventInfo(new LatLng(51.25, 4.15), "Future Event", new Date(1032, 5, 25), "Convention");
		//this date constructor is deprecated but it's just to make a simple example
		
		
		
		Marker firstMarker= mapFragment.placeMarker(firstEventInfo);
		Marker secondMarker = mapFragment.placeMarker(secondEventInfo);
		
		eventMarkerMap = new HashMap<Marker, EventInfo>();
		eventMarkerMap.put(firstMarker, firstEventInfo);
		eventMarkerMap.put(secondMarker, secondEventInfo);
		
		
		//add the onClickInfoWindowListener
		mapFragment.getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
			
			@Override
			public void onInfoWindowClick(Marker marker) {
				EventInfo eventInfo = eventMarkerMap.get(marker);
				Toast.makeText(getBaseContext(),
						"The date of " + eventInfo.getName() + " is " + eventInfo.getSomeDate().toLocaleString(),
						Toast.LENGTH_LONG).show();
				
			}
		});
		
		

		mapFragment.getMap().setInfoWindowAdapter(new InfoWindowAdapter() {
			
			private final View window = getLayoutInflater().inflate(R.layout.custom_window, null);
			
			@Override
			public View getInfoWindow(Marker marker) {
				EventInfo eventInfo = eventMarkerMap.get(marker);
				
				String title = marker.getTitle();
	            TextView txtTitle = ((TextView) window.findViewById(R.id.txtInfoWindowTitle));
	            if (title != null) {
	                // Spannable string allows us to edit the formatting of the text.
	                SpannableString titleText = new SpannableString(title);
	                titleText.setSpan(new ForegroundColorSpan(Color.RED), 0, titleText.length(), 0);
	                txtTitle.setText(titleText);
	            } else {
	                txtTitle.setText("");
	            }
	            
	            TextView txtType = ((TextView) window.findViewById(R.id.txtInfoWindowEventType));
	           if(eventInfo.getType() != null)
	        	   	txtType.setText(eventInfo.getType());
	            
				return window;
			}
			
			@Override
			public View getInfoContents(Marker marker) {
				//this method is not called if getInfoWindow(Marker) does not return null
				return null;
			}
		});
		
	}

	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub

	}
}
